package cn.zzuisa.controller;


import cn.zzuisa.base.PageRequest;
import cn.zzuisa.base.R;
import cn.zzuisa.config.TokenManager;
import cn.zzuisa.entity.Member;
import cn.zzuisa.entity.Reply;
import cn.zzuisa.service.InvitationService;
import cn.zzuisa.service.MemberService;
import cn.zzuisa.service.ReplyService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author frank
 * @since 2019-05-07
 */
@Api(tags = "回复管理")
@RestController
@RequestMapping("/reply")
public class ReplyController {

    @Autowired
    ReplyService replyService;

    @Autowired
    InvitationService invitationService;

    @Autowired
    MemberService memberService;

    // 回复标记已读
    @PutMapping("/status")
    public R<String> status(@RequestBody Long[] ids) {
        if(ids == null) {
            return R.error("不能为空");
        }
        for (Long id: ids) {
            Reply reply = replyService.getById(id);
            reply.setStatus(1);
            replyService.updateById(reply);
        }
        return R.ok("已读");
    }

    @ApiOperation(("某个帖子的回复"))
    @PostMapping("/add")
    public R<Reply> add(@RequestBody Reply reply,HttpServletRequest request) {
        if(reply == null || reply.getInvitationId() == null) {
            return R.error(null);
        }

        Long userId = TokenManager.get(request.getHeader("token"));
        Member member = memberService.getById(userId);
        int i = member.getExperience();
        int level = member.getLevel();
        if(level < 11) {
            if(level * 100 <= i + 10) {
                member.setLevel(level + 1);
                member.setExperience(i + 10 - level * 100);
            }else {
                member.setExperience(i + 10);
            }
            memberService.updateById(member);
        }
        reply.setMemberId(userId);
        reply.setReplyTime(LocalDateTime.now());
        replyService.save(reply);
        return R.ok(reply);
    }

    @ApiOperation("查询我的所有回复     点进去已读回显")
    @GetMapping("/my_list")
    public R<IPage<Map<String,Object>>> replys(PageRequest pageRequest, HttpServletRequest request) {
        IPage<Map<String,Object>> page = new Page<>(pageRequest.getCurrent(),pageRequest.getSize());
        Long userId = TokenManager.get(request.getHeader("token"));
        IPage<Map<String,Object>> replys = replyService.pageSelfReplys(page,userId);
        return R.ok(replys);
    }

    @ApiOperation("查询某个帖子的所有回复")
    @GetMapping("list1")
    public R<IPage<Map<String,Object>>> replys(PageRequest pageRequest, Long invitationId) {
        IPage<Map<String,Object>> page = new Page<>(pageRequest.getCurrent(),pageRequest.getSize());
        IPage<Map<String,Object>> replys = replyService.pageSelfReplys1(page,invitationId);
        return R.ok(replys);
    }

    // 删除回复
    @DeleteMapping("/ids")
    public R<String> delete(Long id) {
        if(id == null) {
            return R.error("参数不能为空");
        }
        replyService.removeById(id);
        return R.ok("删除成功");
    }


}
